package _2022.day._007_346_滑动窗口的平均值;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author： YHSimon
 * @date： 2022-07-16 11:12
 */
public class MovingAverage {
    private Deque<Integer> queue;
    private int capacity;
    private double sum;

    public MovingAverage(int size) {
        this.queue = new LinkedList<>();
        this.capacity = size;
        this.sum = 0;
    }

    public double next(int val) {
        if (queue.size() < capacity) {
            queue.add(val);
            sum += val;
        } else {
            sum -= queue.removeFirst();
            queue.add(val);
            sum += val;
        }
        return sum / queue.size();
    }

    public static void main(String[] args) {
        MovingAverage movingAverage = new MovingAverage(3);
        System.out.println(movingAverage.next(1));
        System.out.println(movingAverage.next(10));
    }

}
